﻿var ns = namespace("kaskelot.sp.ui");

ns.adminfalt = function () {

	function getList() {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/GetAllaFalt",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetAllaFaltResult.HasError) {
                		$("#falt-results tbody").html(
							$("#falt-template").render(data.GetAllaFaltResult.Data)
						);
                	}
                },
			error:
                function (xhr) {
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showCreateForm() {
		var selector = "#falt-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "FaltForm",
				idString: "",
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#falt-form").dialog({ title: "Skapa fält" });
				$(".chzn-select-deselect").chosen({ disable_search: true });
				toggleFaltAlternativ();
				$("#falt-form #Typ").change(toggleFaltAlternativ);
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateFalt";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showEditForm(id) {
		var selector = "#falt-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "FaltForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$(selector).dialog({ title: "Redigera fält" });
				$(".chzn-select-deselect").chosen({ disable_search: true });
				toggleFaltAlternativ();
				$("#falt-form #Typ").change(toggleFaltAlternativ);
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "UpdateFalt";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showDeleteForm(id) {
		var selector = "#falt-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "FaltForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#falt-form").dialog({ title: "Ta bort fält" });
				$("#falt-form #Namn").attr("disabled", "disabled");
				$("#falt-form #Typ").attr("disabled", "disabled");
				$("#falt-form #Default").attr("disabled", "disabled");
				$("#falt-form #FaltAlternativ").attr("disabled", "disabled");
				$("#falt-form #Required").attr("disabled", "disabled");
				$(".chzn-select-deselect").chosen({ disable_search: true });
				toggleFaltAlternativ();
				$("#falt-form #Typ").change(toggleFaltAlternativ);
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "DeleteFalt";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	var originalValue = "";

	function toggleFaltAlternativ() {
		var select = $("#Typ");
		var selectedValue = select.val();
		var textarea = $("#FaltAlternativ");

		if (selectedValue == 5 || selectedValue == 6) {
			textarea.parent().show();
			if (textarea.val() === "" && originalValue != "")
				textarea.val(originalValue);
		}
		else {
			originalValue = textarea.val();
			textarea.val("");
			textarea.parent().hide();
		}
	}

	return {
		getList: getList,
		showEditForm: showEditForm,
		showCreateForm: showCreateForm,
		showDeleteForm: showDeleteForm
	};
}();